/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.txt.access.capability.demo.client;

import it.txt.access.capability.commons.signer.model.X509DocumentSignerInfo;
import it.txt.access.capability.demo.client.impl.CapabilityAuthDemoClientImpl;
import it.txt.access.capability.demo.client.model.CapabilityAuthDemoClientModel;
import it.txt.access.capability.demo.schema.CapabilityRequestType;
import it.txt.access.capability.demo.schema.CapabilityResponseType;
import it.txt.access.capability.demo.service.ICapabilityAuthDemoService;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.osgi.framework.ServiceReference;

/**
 *
 * @author seccia
 */
public class CapabilityAuthDemoClientActivator implements BundleActivator{

    private static final Logger LOGGER = Logger.getLogger(CapabilityAuthDemoClientActivator.class.getName());
    public static final String CAP_CLIENT_ID = "it.txt.access.capability.demo.client.client_id";
    public static final String CAP_OPERATION_ID = "it.txt.access.capability.demo.client.operation_id";
    public static final String CAP_RESOURCE_ID = "it.txt.access.capability.demo.client.resource_id";
    public static final String CAP_CAPABILITY_PATH = "it.txt.access.capability.demo.client.capability_path";
    public static final String CAP_SCHEMAS_DIR = "it.txt.access.capability.demo.client.schemas_dir";
    public static final String CAP_KEYSTORE_PWD = "it.txt.access.capability.demo.client.keystore_pwd";
    public static final String CAP_KEYSTORE_PATH = "it.txt.access.capability.demo.client.keystore_path";
    public static final String CAP_PRIVATE_KEY_PWD = "it.txt.access.capability.demo.client.private_key_pwd";
    private static ClassLoader contextClassLoader;

    public void start(BundleContext context) throws Exception {

        long id = context.getBundle().getBundleId();

        LOGGER.log(Level.INFO, "[TXTAccess :: Capability :: Applications "
                + ":: Demos :: OSGi :: Client]: Bundle ID: {0}", id);

        setContextClassLoader(CapabilityAuthDemoClientActivator.class.getClassLoader());

        Properties configProperties = new Properties();
        configProperties.load(new FileInputStream("conf/config.properties"));

        String capabilitySchemasPath = configProperties.getProperty(CAP_SCHEMAS_DIR);
        String capabilityPath = configProperties.getProperty(CAP_CAPABILITY_PATH);
        String operationID = configProperties.getProperty(CAP_OPERATION_ID);
        String resourceID = configProperties.getProperty(CAP_RESOURCE_ID);
        
        X509DocumentSignerInfo clientInfo = new X509DocumentSignerInfo();
        clientInfo.setSignerID(configProperties.getProperty(CAP_CLIENT_ID));
        clientInfo.setKeystorePath(configProperties.getProperty(CAP_KEYSTORE_PATH));
        clientInfo.setKeystorePwd(configProperties.getProperty(CAP_KEYSTORE_PWD).toCharArray());
        clientInfo.setPrivateKeyPwd(configProperties.getProperty(CAP_PRIVATE_KEY_PWD).toCharArray());

        CapabilityAuthDemoClientModel model = new CapabilityAuthDemoClientModel();
        model.setCapabilitySchemasPath(capabilitySchemasPath);
        model.setX509DocumentSignerInfo(clientInfo);
        model.setCapabilityPath(capabilityPath);
        model.setOperationID(operationID);
        model.setResourceID(resourceID);
        CapabilityAuthDemoClientImpl authDemoClientImpl = new CapabilityAuthDemoClientImpl(model);

        if(context.getServiceReference(ICapabilityAuthDemoService.class.getName()) == null){
            LOGGER.log(Level.INFO, "[TXTAccess :: Capability :: Applications "
                + ":: Demos :: OSGi :: Client]: Serviced Reference null");
        }
        else{
            ServiceReference ref = context.getServiceReference(ICapabilityAuthDemoService.class.getName());
            
            ICapabilityAuthDemoService service = (ICapabilityAuthDemoService) context.getService(ref);
            CapabilityRequestType crt = authDemoClientImpl.getNewCapabilityRequestClient();
            CapabilityResponseType responseType = service.capabilityRequestService(crt);
            authDemoClientImpl.checkCapabilityAuthResponse(responseType);
        }

    }

    public void stop(BundleContext context) throws Exception {
        return;
    }

    public static ClassLoader getContextClassLoader() {
        return contextClassLoader;
    }

    public static void setContextClassLoader(ClassLoader contextClassLoader) {
        CapabilityAuthDemoClientActivator.contextClassLoader = contextClassLoader;
    }

}
